package joshua.web.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbConnection {
	private static Connection dbConn;
	private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
	private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
	private static final String DB_USER = "root";
	private static final String DB_PASS= "rootroot";
	
	static {
		try {
			Class.forName(DB_DRIVER);
			dbConn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getDbConn() {
		return dbConn;
	}
	
	
	public static ResultSet executeQuery(String sql) throws SQLException {
		Connection db = getDbConn();
		Statement stmt = db.createStatement();
		return stmt.executeQuery(sql);
	}
	
	
	public static void executeSql(String sql) throws SQLException {
		Connection db = getDbConn();
		Statement stmt = db.createStatement();
		stmt.execute(sql);
	}
}
